package com.bjpower.services.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpower.common.constants.Sysconts;
import com.bjpower.common.enums.PublicStatus;
import com.bjpower.common.enums.SysCode;
import com.bjpower.common.exceptions.type.SysException;
import com.bjpower.domain.PayProduct;
import com.bjpower.mapper.PayProductMapper;
import com.bjpower.models.request.ProductAddRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.services.ProductListService;
import com.bjpower.utlis.RedisHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductListServiceImpl implements ProductListService {

    @Resource
    private PayProductMapper payProductMapper;

    @Resource
    private RedisHelper redisHelper;

    @Override
    public IPage<PayProduct> getPayProductList(UserPageInfoRequest pageInfoRequest) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("edit_time");
        IPage<PayProduct> payProductIPage = payProductMapper.selectPage(
                Page.of(pageInfoRequest.getPageNum(), pageInfoRequest.getPageSize()), wrapper);

        return payProductIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addPayProductService(ProductAddRequest productAddRequest) {
        //商品编号只能有一个
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_code", productAddRequest.getCode());
        if (payProductMapper.selectCount(queryWrapper) > 0) {
            throw new SysException(SysCode.PRODUCT_CODE_EXIST);
        }
        //商品名字只能有一个
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("product_name", productAddRequest.getName());
        if (payProductMapper.selectCount(wrapper) > 0) {
            throw new SysException(SysCode.PRODUCT_NAME_EXIST);
        }
        //添加商品
        PayProduct payProduct = new PayProduct();
        //创建uuId
        payProduct.setId(IdWorker.get32UUID());
        payProduct.setProductCode(productAddRequest.getCode());
        payProduct.setProductName(productAddRequest.getName());
        payProduct.setStatus(PublicStatus.ENABLE.name());
        payProduct.setEditTime(new Date());
        payProduct.setRemark("创建新的支付产品");
        payProductMapper.insert(payProduct);

        //将商品信息放入redis中
        JSONObject object = new JSONObject();
        object.set("code", productAddRequest.getCode());
        object.set("name", productAddRequest.getName());
        redisHelper.setList(Sysconts.PAY_PRODUCT_LIST, object.toJSONString(0));
    }

    @Override
    public List<PayProduct> selectMerchProductList(String productName) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.select("product_code", "product_name").like("product_name", productName);
        List<PayProduct> payProducts = payProductMapper.selectList(wrapper);
        return payProducts;
    }
}
